// Estimate regressions in Figure 1
// Document polarization in our sample on national policy issues

clear all
set more off

cd "$localdir/Data"
gl output "$localdir/Output"


set scheme s1mono

clear all 
set more off

use msa_survey_indiv.dta, clear




rename party_id party_id_nolean
clonevar party_id_lean = party_id_nolean
replace party_id_lean = 1 if  q41_b == 2
replace party_id_lean = 2 if  q41_b == 1

rename party_strong pid7
lab def pid7 1 "Strong Democrat" 2 "Democrat" 3 "Lean Democrat" 4 "Independent" 5 "Lean Republican" 6 "Republican" 7 "Strong Republican"
lab val pid7 pid7

gen dem_lean = party_id_lean == 1
lab var dem_lean "Democrat"
gen rep_lean = party_id_lean == 2
lab var rep_lean "Republican"
gen ind_lean = party_id_lean == 3
lab var ind_lean "Independent"

gen looking_for_work = 0
replace looking_for_work = 1 if empl_status == 4 | empl_status == 3

egen years_in_msa2 = cut(years_in_msa), at(0, 1, 5, 10, 15, 100) 

gen homeowner = own_where_living == 1

// generate msa-specific income quartile variable
levelsof msa
gen inc_quartile = .
foreach l in `r(levels)' {
	cap drop temp
	sum income if msa == `l', detail
	egen temp = cut(income) if msa == `l', at(`r(min)', `r(p25)', `r(p50)', `r(p75)', `r(max)') icodes
	replace inc_quartile = temp if msa == `l'
}
cap drop temp


// Perceptions of the economy - not actually reported in the paper
foreach stub in us_econ msa_econ personal_fin {
	
	local v = "dissatisfied_" + "`stub'"
	
	reg `v'  ind_lean rep_lean [pw=weight], robust
	estimates store `stub'_biv
	
	reg `v' ind_lean rep_lean age3150-agegt65 i.income i.race female college i.msa [pw=weight], robust
	estimates store `stub'_mult
}
coefplot (us_econ_biv, msymbol(circle) mcolor(black) label(US Econ w/o controls)) ///
		 (us_econ_mult, msymbol(circle) mcolor(gs10) label(US Econ - w/ controls)) ///
		 (msa_econ_biv, msymbol(square) mcolor(black) label(MSA Econ - w/o controls)) ///
		 (msa_econ_mult, msymbol(square) mcolor(gs10) label(MSA Econ - w/o controls)), ///
		 keep(*lean) xline(0, lpattern(dot))  ///
		 title("Partisan bias in evaluation of the economy") /// 
		 xtitle("Dissatisfaction with the economy relative to Democrats (1-10 scale)")
*graph export figs/econ_dissatisfaction_partisan.pdf, replace



// National policy 
// make these items binary for ease of interpretation
gen ineq_smaller_bin = inequality_smaller == 1 | inequality_smaller == 2
lab var ineq_smaller_bin "Want more/same inequality"

gen safety_net_bin = gov_more_safety_net == 1 | gov_more_safety_net == 2
lab var safety_net_bin "Wants to spend less on safety net"

gen reduce_trade_bin = reduce_trade == 4 | reduce_trade == 5
lab var reduce_trade_bin "Wants to reduce trade"

gen for_investment_bin = for_investment == 2
lab var for_investment_bin "Wants to reduce foreign investment" 

gen reduce_immig_bin = reduce_immig == 4 | reduce_immig == 5
lab var reduce_immig_bin "Wants to reduce immigration"


local ind_covs = "i.pid7 age3150 age5165 agegt65 black latino other female college i.inc_quartile looking_for_work homeowner i.years_in_msa2 i.msa"

mata: b = J(5, 3, 0)
local i = 1
foreach v of varlist ineq_smaller_bin safety_net_bin reduce_trade_bin for_investment_bin reduce_immig_bin {
	reg `v'  i.pid7 [pw=weight], robust
	estimates store `v'_biv
	
	reg `v' `ind_covs', robust
	estimates store `v'_mult
	
	local b = _b[5.pid7]
	local se = _se[5.pid7]
	
	mata: b[`i', 1] = `i'
	mata: b[`i', 2] = `b'
	mata: b[`i', 3] = `se'
	
	local ++i
}

// coefplot to quickly look at results -- actual plot is made in separate R file
coefplot (reduce_immig_bin_mult, keep(*pid*) base) ///
		 (ineq_smaller_bin_mult, keep(*pid*) base) ///
		 (for_investment_bin_mult, keep(*pid*) base) ///
		 (reduce_trade_bin_mult, keep(*pid*) base) ///
		 (safety_net_bin_mult, keep(*pid*) base) , ///
		 plotlabels("Reduce immigration" "Don't make inequality smaller" "Reduce foreign investment" "Reduce trade" "Reduce safety net") 
*graph export figs/national_policy_polarization.pdf, replace


// a few other categorical outcomes
mlogit policy_approach_trade `ind_covs'
test i2.pid7 i3.pid7  i4.pid7 i5.pid7 i6.pid7 i7.pid7 

mlogit policy_approach_tech `ind_covs'
test i2.pid7 i3.pid7  i4.pid7 i5.pid7 i6.pid7 i7.pid7 


// Save coefficients that are stored in "b" matrix 
clear
getmata (depvar beta se) = b, force
lab def depvar 1 "Don't make inequality smaller" 2 "Reduce safety net" 3 "Reduce trade"  4 "Reduce foreign investment" 5 "Reduce immigration"
lab val depvar depvar
decode depvar, gen(depvar2) 
drop depvar
rename depvar2 depvar
outsheet using "polarization_coefs.csv", replace comma
